package com.tfswx.fljdmxyy.service.impl;

import com.newbie.core.exception.BusinessException;
import com.tfswx.fljdmxyy.dao.QxXtGnjkMapper;
import com.tfswx.fljdmxyy.dao.QxXtJkdyMapper;
import com.tfswx.fljdmxyy.dto.gnjk.GnjkAddInputDTO;
import com.tfswx.fljdmxyy.dto.gnjk.GnjkAddOutputDTO;
import com.tfswx.fljdmxyy.dto.gnjk.GnjkDeleteInputDTO;
import com.tfswx.fljdmxyy.dto.gnjk.GnjkUpdateInputDTO;
import com.tfswx.fljdmxyy.enums.BusinessCodeEnum;
import com.tfswx.fljdmxyy.model.QxXtGnjk;
import com.tfswx.fljdmxyy.model.QxXtJkdy;
import com.tfswx.fljdmxyy.query.gnjk.GnjkQueryParam;
import com.tfswx.fljdmxyy.query.gnjk.GnjkQueryResult;
import com.tfswx.fljdmxyy.service.GnjkService;
import com.tfswx.fljdmxyy.utils.UuidUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 功能接口业务实现类
 *
 * @author: 陈涛
 * @date: 2022-11-11
 **/
@Slf4j
@Service
public class GnjkServiceImpl implements GnjkService {

    @Resource
    private QxXtGnjkMapper gnjkMapper;

    @Resource
    private QxXtJkdyMapper jkdyMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GnjkAddOutputDTO addGnfl(GnjkAddInputDTO dto) {
        Date now = new Date();
        // 1.新增接口定义
        QxXtJkdy qxXtJkdy = new QxXtJkdy();
        qxXtJkdy.setJkbm(UuidUtils.uuid32());
        qxXtJkdy.setJkdz(dto.getJkdz());
        qxXtJkdy.setJkmc(dto.getJkmc());
        qxXtJkdy.setJkms(dto.getJkms());
        qxXtJkdy.setQxbjqdz(dto.getQxbjqdz());
        qxXtJkdy.setCjsj(now);
        qxXtJkdy.setZhxgsj(now);
        int insert = jkdyMapper.insertJkdy(qxXtJkdy);
        if (insert <= 0) {
            throw new BusinessException(BusinessCodeEnum.TJSJSB);
        }

        // 2.新增功能接口
        QxXtGnjk qxXtGnjk = new QxXtGnjk();
        qxXtGnjk.setGnbm(dto.getGnbm());
        qxXtGnjk.setJksjqx(dto.getJksjqx());
        qxXtGnjk.setJkbm(qxXtJkdy.getJkbm());
        qxXtGnjk.setCjsj(now);
        qxXtGnjk.setZhxgsj(now);
        insert = gnjkMapper.insertGnjk(qxXtGnjk);
        if (insert <= 0) {
            throw new BusinessException(BusinessCodeEnum.TJSJSB);
        }

        // 3.返回结果
        GnjkAddOutputDTO result = new GnjkAddOutputDTO();
        result.setJkbm(qxXtGnjk.getJkbm());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteGnjk(GnjkDeleteInputDTO dto) {
        Date now = new Date();
        // 1.删除功能接口
        QxXtGnjk qxXtGnjk = new QxXtGnjk();
        qxXtGnjk.setJkbm(dto.getJkbm());
        qxXtGnjk.setZhxgsj(now);
        int delete = gnjkMapper.deleteGnjk(qxXtGnjk);
        if (delete <= 0) {
            throw new BusinessException(BusinessCodeEnum.SCSJSB);
        }

        // 2.删除接口定义
        QxXtJkdy qxXtJkdy = new QxXtJkdy();
        qxXtJkdy.setJkbm(dto.getJkbm());
        qxXtJkdy.setZhxgsj(now);
        delete = jkdyMapper.deleteJkdy(qxXtJkdy);
        if (delete <= 0) {
            throw new BusinessException(BusinessCodeEnum.SCSJSB);
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateGnjk(GnjkUpdateInputDTO dto) {
        QxXtJkdy qxXtJkdy = new QxXtJkdy();
        qxXtJkdy.setJkbm(dto.getJkbm());
        qxXtJkdy.setZhxgsj(new Date());
        qxXtJkdy.setJkdz(dto.getJkdz());
        qxXtJkdy.setJkmc(dto.getJkmc());
        qxXtJkdy.setJkms(dto.getJkms());
        qxXtJkdy.setQxbjqdz(dto.getQxbjqdz());
        int update = jkdyMapper.updateJkdy(qxXtJkdy);
        if (update <= 0) {
            throw new BusinessException(BusinessCodeEnum.XGSJSB);
        }
        return Boolean.TRUE;
    }

    @Override
    public List<GnjkQueryResult> queryGnjk(GnjkQueryParam param) {
        return gnjkMapper.queryGnjk(param);
    }
}
